package com.aidl.demo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.loyal.qr.ui.PortActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, UpdateService.class);
        intent.putExtra("name", "Java");
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    private IMyAidlInterface mIMyAidlInterface;

    ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mIMyAidlInterface = null;
        }
    };

    public void onClick(View view) {
        if (null != mIMyAidlInterface) {
            try {
                String name = mIMyAidlInterface.getName();
                Log.e(TAG, "aidl.getName: " + name);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        //startActivityForResult(new Intent(this, TestActivity.class), 200);
    }

    public void onQrClick(View view) {
        startActivityForResult(new Intent(this, PortActivity.class), 200);
    }
}
